package com.sidneysimmons.financially.adapters;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.sidneysimmons.financially.Category;
import com.sidneysimmons.financially.R;

public class CategoryArrayAdapter extends ArrayAdapter<Category> {
	private final String CLASS_NAME = this.getClass().getName();
	
	private Context context;
	private Category[] categories;
	
	public CategoryArrayAdapter(Context context, Category[] categories) {
		super(context, R.layout.category_row, categories);
		
		this.context = context;
		this.categories = categories;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// inflate the row layout
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View rowView = inflater.inflate(R.layout.category_row, parent, false);
		
		// grab and set text
		TextView textView = (TextView)rowView.findViewById(R.id.category_row_title);
		textView.setText(categories[position].getTitle());
		
		return rowView;
	}
	
	/**
	 * Get the Category in the array at this position.  Note: this returns a reference to 
	 * the object in the array, so don't change it unless you really want to.
	 * 
	 * @param position - The position in the array.
	 * 
	 * @return The Category, or null if the position is out of bounds.
	 */
	public Category getCategory(int position) {
		if (position >= categories.length) {
			Log.e(CLASS_NAME, "You can't get a Category at an out of bounds position.");
			Log.e(CLASS_NAME, "categories[].length: " + categories.length);
			Log.e(CLASS_NAME, "requested index: " + position);
			new Exception().printStackTrace();
			return null;
		} else {
			return categories[position];
		}
	}
}